{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#取数据\n",
    "import numpy as np\n",
    "x_data = [ 338., 333., 328., 207., 226., 25., 179., 60., 208., 606.]\n",
    "y_data = [ 640., 663., 619., 393., 428., 27., 193., 66., 226., 1591.]\n",
    "\n",
    "n = 1\n",
    "X = np.ones((len(x_data),n + 1), dtype= np.float)\n",
    "Y = np.zeros((len(y_data)), dtype= np.float)\n",
    "\n",
    "\n",
    "row = 0\n",
    "for x in X:\n",
    "    x[0] = x_data[row]\n",
    "    #print(x)\n",
    "    row += 1\n",
    "#print(X)\n",
    "\n",
    "for i in range(0, len(y_data)):\n",
    "    Y[i] = y_data[i]\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "shapes (10,2) and (9,1) not aligned: 2 (dim 1) != 9 (dim 0)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-8b4445b36f70>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      8\u001b[0m     \u001b[0mw_grad\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[0mWt\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtranspose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mW\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m     \u001b[0mXmulW\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmatmul\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mW\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m     \u001b[1;31m#print(\"Xt Shape:\" + str(Xt.shape))\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m     \u001b[0mXtMatrix\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmatrix\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mXt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: shapes (10,2) and (9,1) not aligned: 2 (dim 1) != 9 (dim 0)"
     ]
    }
   ],
   "source": [
    "#递归下降求解\n",
    "itor = 10000\n",
    "lr = 0.00001\n",
    "W = np.zeros((2,1), dtype = np.float)\n",
    "Xt = np.transpose(X)\n",
    "Yt = np.transpose(np.matrix(Y))\n",
    "for i in range(0, itor):\n",
    "    w_grad = 0\n",
    "    Wt = np.transpose(W)\n",
    "    XmulW = np.matmul(X,W)\n",
    "    #print(\"Xt Shape:\" + str(Xt.shape))\n",
    "    XtMatrix = np.matrix(Xt)\n",
    "    #print(\"XtMatrix Shape:\" + str(XtMatrix.shape))\n",
    "    #print(\"Yt Shape:\" + str(Yt.shape))\n",
    "    #print(\"XmulW Shape:\" + str(XmulW.shape))\n",
    "\n",
    "    XWdecYMatrix = np.matrix( XmulW - Yt)\n",
    "    #print(XWdecYMatrix.shape)\n",
    "    grad = np.matmul(2 * Xt, XmulW - Yt)\n",
    "    #print(grad.shape)\n",
    "    W -= grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#画数据分布图\n",
    "Yhead = np.matmul(W, np.transpose(X))\n",
    "print(type(Yhead[0].tolist()))\n",
    "print()\n",
    "import matplotlib.pyplot as plt  # plt\n",
    "x = np.arange(0, 800, 8) # x\n",
    "y = np.arange(0, 2000,20) # y\n",
    "#x = np.arange(100, 200, 1) # bias\n",
    "#y = np.arange(-5, 5, 0.1) # weight\n",
    "Z = np.zeros((len(x), len(y)))\n",
    "# plot the figure\n",
    "plt.contourf(x, y, Z, 50, alpha=0.5, cmap=plt.get_cmap('jet'))\n",
    "# 李宏毅课程原代码为markeredeweight=3,无法运行，改为了marker=3。\n",
    "# ms和marker分别代表指定点的长度和宽度。\n",
    "plt.plot(x_data, y_data, 'o', ms=3, lw=1.5, color='black')\n",
    "plt.plot(x_data, Yhead.tolist()[0], 'o-', ms=3, lw=1.5, color='green')\n",
    "\n",
    "plt.xlim(0, 800)\n",
    "plt.ylim(0, 2000)\n",
    "plt.xlabel(r'$b$', fontsize=16)\n",
    "plt.ylabel(r'$w$', fontsize=16)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#判断的出结果的好坏\n",
    "print(W)\n",
    "Yhead = np.matmul(W, np.transpose(X))\n",
    "L = Yhead - Y\n",
    "sum = 0\n",
    "row = 0\n",
    "for i in range(0,L.shape[1]):\n",
    "    print(str(Y[i]) + \" : \"+ str(Yhead[0, i]) + \" L: \" + str(L[0,i]))\n",
    "    sum += L[0,i]*L[0,i] \n",
    "    row += 1\n",
    "print(\"L:\" + str(sum/L.shape[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
